﻿using Microsoft.EntityFrameworkCore;
using System;
using WebApiOrm.Provider;
using WebApiOrm.Provider.EFCoreSqlite;

namespace Microsoft.Extensions.DependencyInjection
{
    /// <summary>
    /// OrmBuilder扩展
    /// 扩展添加具体的Orm
    /// </summary>
    public static class OrmBuilderExtensions
    {
        /// <summary>
        /// 添加EFCore的Orm
        /// </summary>
        /// <param name="builder"></param>
        /// <param name="ormName"></param> 
        /// <returns></returns>
        public static IOrmBuilder AddEFCore(this IOrmBuilder builder, string ormName)
        {
            builder.Services.AddOrmProvider<EFCoreOrmProvider>(ormName);
            builder.Services.AddDbContextPool<EFCoreDbContext>(db =>
            {
                switch (builder.DatabaseType)
                {
                    case DatabaseType.Sqlite:
                        db.UseSqlite(builder.ConnectionString);
                        break;

                    case DatabaseType.SqlServer:
                        db.UseSqlServer(builder.ConnectionString);
                        break;

                    case DatabaseType.PostgreSQL:
                        db.UseNpgsql(builder.ConnectionString);
                        break;

                    default:
                        throw new NotSupportedException();
                }
            });

            return builder;
        }
    }
}
